<template>
  <div class="form-single-fragment" style="position: relative;">
    <h3>编辑</h3>
    <el-form :model="ruleForm" ref="ruleForm" label-width="180px" size="mini" label-position="right">
      <el-row :gutter="20" class="form-single-info">
        <el-col :span="20">
          <div class="title"><h4>项目信息</h4></div>
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目名称：">{{detailData.projectName || '--'}}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目编号：">{{detailData.projectCode || '--'}}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所在地区：">{{detailData.areaName || '--'}}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="合同金额（元）：">{{detailData.contractAmount || '--'}}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单位名称：">{{detailData.customerName || '--'}}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="业务类型：">{{detailData.businessTypeName || '--'}}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="合同签约时间：">{{detailData.signingDate || '--'}}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="合同完成时间：">{{detailData.finishDate || '--'}}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实际完成时间：">{{detailData.finishDate || '--'}}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目状态：">{{detailData.projectStatusName || '--'}}</el-form-item>
            </el-col>
          </el-row>
          <div class="title">
            <h4>成员评分</h4>
          </div>
          <el-table ref="table" :data="ruleForm.personalScoreDoList" size="mini" style="width: 100%;" header-cell-class-name="table-header-gray">
            <el-table-column type="index" align="center" width="55" label="序号"></el-table-column>
            <el-table-column prop="userName" align="center" label="姓名"></el-table-column>
            <el-table-column prop="deptName" align="center" label="部门"></el-table-column>
            <el-table-column prop="postName" align="center" label="岗位"></el-table-column>
            <el-table-column prop="createTime" align="center" label="评分">
              <template slot="header">
                <span>评分<span style="color:red;font-size:16px;">*</span></span>
              </template>
              <template slot-scope="scope">
                <el-form-item class="table_input" :prop="'personalScoreDoList.' + scope.$index + '.score'" :rules="rules.score">
                  <el-input type="number" class="input-item"
                  v-model="scope.row.score"
                  placeholder="请输入"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
          </el-table>
          <div class="title info">
            <span>评分说明：<br/>
            根据项目成员在项目中发挥的实际贡献情况进行评分，所有成员评分合计为100分。</span>
          </div>
        </el-col>
      </el-row>
      <el-form-item style="padding: 20px 0;">
        <el-button size="mini" @click="resetForm">返回</el-button>
        <el-button size="mini" type="primary" @click="sumscore">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Administration, Contracts } from '@/api/projectAdministration.js';
import { Personalintergral } from '@/api/integrals.js';
export default {
  name: 'PersonalintergralEdit',
  data () {
    return {
      loading: false,
      isShowloading: false,
      detailData: {},
      ruleForm: {
        personalScoreDoList: []
      },
      rules: {
        score: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    };
  },
  created () {
    this.loadProjectDetailWidgetData()
    this.loadUserWidgetData()
  },
  methods: {
    /**
    * 项目详情
    */
    loadProjectDetailWidgetData () {
      let params = {
        id: this.$route.query.projectId
      };
      return new Promise((resolve, reject) => {
        Administration.detail(this, params).then(res => {
          this.detailData = res.data
          if (this.detailData.contractId) {
            this.loadContractDetailWidgetData()
          }
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
    * 合同简介
    */
    loadContractDetailWidgetData () {
      let params = {
        id: this.detailData.contractId
      };
      return new Promise((resolve, reject) => {
        Contracts.detail(this, params).then(res => {
          this.detailData.contractAmount = res.data.contractAmount
          this.detailData.signingDate = res.data.signingDate
          this.detailData.finishDate = res.data.finishDate
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 项目成员列表
     */
    loadUserWidgetData () {
      let params = {
        deptId: this.$route.query.deptId,
        projectId: this.$route.query.projectId
      }
      return new Promise((resolve, reject) => {
        Personalintergral.list(this, params).then(res => {
          this.ruleForm.personalScoreDoList = res.data.records
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 成员分数总和
     */
    sumscore () {
      let scoresum = 0
      this.ruleForm.personalScoreDoList.forEach(item => {
        scoresum = Number(scoresum) + Number(item.score)
      })
      if (scoresum === 100) {
        this.submitForm()
      } else {
        this.$message({
          showClose: true,
          message: '评分合计为100分！',
          type: 'warning'
        });
      }
    },
    getstageAmount () {
      let contractStageDtos = this.ruleForm.personalScoreDoList
      let stageAmount = 0
      contractStageDtos.forEach(item => {
        stageAmount = Number(stageAmount) + Number(item.score)
      })
      this.ruleForm.contractAmount = stageAmount;
    },
    /**
     * 成员单个评分
     */
    submitfirstForm () {
      this.isShowloading = true;
      let params = this.ruleForm;
      return new Promise((resolve, reject) => {
        Personalintergral.update(this, params).then(res => {
          this.$message({
            showClose: true,
            message: '操作成功！',
            type: 'success'
          });
          this.resetForm();
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 提交表单
     */
    submitForm () {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.isShowloading = true;
          let params = this.ruleForm.personalScoreDoList;
          return new Promise((resolve, reject) => {
            Personalintergral.batchSave(this, params).then(res => {
              this.$message({
                showClose: true,
                message: '操作成功！',
                type: 'success'
              });
              this.resetForm();
            }).catch(e => {
              reject(e);
            });
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm () {
      this.$router.go(-1)
    }
  }
}
</script>

<style lang="scss" scoped>
h3{border-bottom: 1px solid #E9E9EB;padding: 0 0 10px;}
.table_container{margin: 10px 0;}
.form-single-info{display: flex;justify-content: center;}
.el-form-info{box-shadow: 0 0 10px rgba(0,0,0,0.05);padding: 20px;}
.el-form-info strong{margin: 0 10px;}
.el-form-info .el-time-picker{width: 100%;}
.title{
  display: flex;align-items: center;justify-content: space-between;
  h4{font-weight: bold;}
}
.info{margin: 10px 0;line-height: 24px;}
.table_input /deep/.el-form-item__content{margin-left: 0 !important;}
.table_input.el-form-item{margin: 12px 0;}
</style>
